//
//  DataBaseHandle.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/16.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DataBaseHandle.h"
#import "TopocModel.h"
#import <FMDB.h>


@interface DataBaseHandle ()
@property (strong,nonatomic) FMDatabase *database;

@end


@implementation DataBaseHandle

+ (instancetype)shareDataBaseHandle{
    static DataBaseHandle *dataBaseHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataBaseHandle = [DataBaseHandle new];
    });
    return dataBaseHandle;
}
//打开数据库
- (void)openDatabase{
    [self.database open];
    
}
//关闭数据库
- (void)closeDatabase{
    [self.database close];
}
//建表
- (void)CreateTable{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSLog(@"路径：%@",path);
    NSString *fileName = [path stringByAppendingPathComponent:@"topicTable.sqlite"];
    self.database = [FMDatabase databaseWithPath:fileName];
    if ([self.database open]) {
        BOOL result = [self.database executeUpdate:@"create table if not exists topicTable (t_id integer primary key autoincrement not null,name text ,OneHead text ,twoHeader text  ,classification text,concernCount text  ,OneHeadImage text ,twoHeadImage text ,talkCount text ,subjectId text ,image1 text ,image2 text ,image3 text);"];
        if (result) {
            NSLog(@"建表成功");
        }else{
            NSLog(@"建表失败");
        }
    }
    
    
    
    
}
//插入
- (void)insterTableWithModel:(TopocModel *)model{
    NSArray *array = model.talkContent;
    NSDictionary *dic = array[0];
    NSDictionary *dic1 = array[1];
    NSString *onehead = dic[@"content"];
    NSString *twoHeader = dic1[@"content"];
    NSString *OneHeadImage = dic[@"userHeadPicUrl"];
    NSString *twoHeadImage = dic1[@"userHeadPicUrl"];
    
    NSArray *arrayImage = model.talkPicture;
    
   BOOL result =  [self.database executeUpdateWithFormat:@"insert into topicTable(name,OneHead,twoHeader,classification,concernCount,OneHeadImage,twoHeadImage,talkCount,subjectId,image1,image2,image3) values (%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@);",model.name,onehead,twoHeader,model.classification,model.concernCount,OneHeadImage,twoHeadImage,model.talkCount,model.subjectId,arrayImage[0],arrayImage[1],arrayImage[2]];
    if (result) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}
//删除
- (void)deleteWithModle:(TopocModel *)model{
    BOOL result = [self.database executeUpdateWithFormat:@"delete from topicTable WHERE name = %@",model.name];
    if (result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
}

//name,OneHead,twoHeader,classification,concernCount,OneHeadImage,twoHeadImage,talkCount,subjectId
//查询
- (NSMutableArray *)select{
    NSMutableArray *array = [NSMutableArray array];
    FMResultSet *resultSet = [self.database executeQuery:@"select * from topicTable"];
    while ([resultSet next]) {
        NSString *name = [resultSet stringForColumn:@"name"];
        NSString *OneHead = [resultSet stringForColumn:@"OneHead"];
        NSString *twoHeader = [resultSet stringForColumn:@"twoHeader"];
        NSString *classification = [resultSet stringForColumn:@"classification"];
        NSString *concernCount = [resultSet stringForColumn:@"concernCount"];
        NSString *OneHeadImage = [resultSet stringForColumn:@"OneHeadImage"];
        NSString *twoHeadImage = [resultSet stringForColumn:@"twoHeadImage"];
        NSString *talkCount = [resultSet stringForColumn:@"talkCount"];
        NSString *subjectId = [resultSet stringForColumn:@"subjectId"];
        NSString *image1 = [resultSet stringForColumn:@"image1"];
        NSString *image2 = [resultSet stringForColumn:@"image2"];
        NSString *image3 = [resultSet stringForColumn:@"image3"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:name forKey:@"name"];
        [dic setValue:OneHead forKey:@"OneHead"];
        [dic setValue:twoHeader forKey:@"twoHeader"];
        [dic setValue:classification forKey:@"classification"];
        [dic setValue:concernCount forKey:@"concernCount"];
        [dic setValue:OneHeadImage forKey:@"OneHeadImage"];
        [dic setValue:twoHeadImage forKey:@"twoHeadImage"];
        [dic setValue:talkCount forKey:@"talkCount"];
        [dic setValue:subjectId forKey:@"subjectId"];
        [dic setValue:image1 forKey:@"image1"];
        [dic setValue:image2 forKey:@"image2"];
        [dic setValue:image3 forKey:@"image3"];
        
        [array addObject:dic];
        
    }
    
    return array;
}


@end
